<?xml version="1.0" encoding="utf-8"?>
<modification>
  <name>Foc Simple Seo</name>
  <code>default</code>
  <version>1.0</version>
  <author>Freeocart</author>
  <link>http://freeocart.ru</link>

  <!-- auto url generate -->
  <file path="admin/view/template/catalog/product_form.twig">
    <operation>
      <search><![CDATA[{{ footer }}]]></search>
      <add position="before"><![CDATA[
        <script type="text/javascript" src="view/javascript/focsseo/focsseo-transliterator.js"></script>
        <script>
        var languages = {}
        {% for language in languages %}
        languages['{{ language.language_id }}'] = {
          source: '#input-name{{ language.language_id }}',
          copyTextTo: [
            '#input-meta-title{{ language.language_id }}'
          ],
          targets: [
            {% for store in stores %}
            {
              selector: '[name="product_seo_url[{{ store.store_id }}][{{ language.language_id }}]"]',
              forceReplace: {% if product_seo_url[store.store_id][language.language_id] %}false{% else %}true{% endif %}
            }{% if not loop.last %},{% endif %}
            {% endfor %}
          ],
          prefix: '{{ language.seo_url_prefix }}',
          postfix: '{{ language.seo_url_postfix }}'
        }
        {% endfor %}

        new FocSSeoTransliterator({
          languages: languages
        });
        </script>
      ]]></add>
    </operation>
  </file>

  <file path="admin/view/template/catalog/category_form.twig">
    <operation>
      <search><![CDATA[{{ footer }}]]></search>
      <add position="before"><![CDATA[
        <script type="text/javascript" src="view/javascript/focsseo/focsseo-transliterator.js"></script>
        <script>
        var languages = {}
        {% for language in languages %}
        languages['{{ language.language_id }}'] = {
          source: '#input-name{{ language.language_id }}',
          copyTextTo: [
            '#input-meta-title{{ language.language_id }}'
          ],
          targets: [
            {% for store in stores %}
            {
              selector: '[name="category_seo_url[{{ store.store_id }}][{{ language.language_id }}]"]',
              forceReplace: {% if category_seo_url[store.store_id][language.language_id] %}false{% else %}true{% endif %}
            }{% if not loop.last %},{% endif %}
            {% endfor %}
          ],
          prefix: '{{ language.seo_url_prefix }}',
          postfix: '{{ language.seo_url_postfix }}'
        }
        {% endfor %}

        new FocSSeoTransliterator({
          languages: languages
        });
        </script>
      ]]></add>
    </operation>
  </file>

  <file path="admin/view/template/catalog/information_form.twig">
    <operation>
      <search><![CDATA[{{ footer }}]]></search>
      <add position="before"><![CDATA[
        <script type="text/javascript" src="view/javascript/focsseo/focsseo-transliterator.js"></script>
        <script>
        var languages = {}
        {% for language in languages %}
        languages['{{ language.language_id }}'] = {
          source: '#input-title{{ language.language_id }}',
          copyTextTo: [
            '#input-meta-title{{ language.language_id }}'
          ],
          targets: [
            {% for store in stores %}
            {
              selector: '[name="information_seo_url[{{ store.store_id }}][{{ language.language_id }}]"]',
              forceReplace: {% if information_seo_url[store.store_id][language.language_id] %}false{% else %}true{% endif %}
            }{% if not loop.last %},{% endif %}
            {% endfor %}
          ],
          prefix: '{{ language.seo_url_prefix }}',
          postfix: '{{ language.seo_url_postfix }}'
        }
        {% endfor %}

        new FocSSeoTransliterator({
          languages: languages
        });
        </script>
      ]]></add>
    </operation>
  </file>

  <file path="admin/view/template/catalog/manufacturer_form.twig">
    <operation>
      <search><![CDATA[{{ footer }}]]></search>
      <add position="before"><![CDATA[
        <script type="text/javascript" src="view/javascript/focsseo/focsseo-transliterator.js"></script>
        <script>
        var languages = {}
        languages['{{ language.language_id }}'] = {
          source: '#input-name',
          targets: [
          {% for language in languages %}
            {% for store in stores %}
            {
              selector: '[name="manufacturer_seo_url[{{ store.store_id }}][{{ language.language_id }}]"]',
              forceReplace: {% if manufacturer_seo_url[store.store_id][language.language_id] %}false{% else %}true{% endif %}
            }{% if not loop.last %},{% endif %}
            {% endfor %}{% if not loop.last %},{% endif %}
          {% endfor %}
          ],
          prefix: '{{ language.seo_url_prefix }}',
          postfix: '{{ language.seo_url_postfix }}'
        }

        new FocSSeoTransliterator({
          languages: languages
        });
        </script>
      ]]></add>
    </operation>
  </file>

  <!-- for fractalika -->
  <file path="admin/view/template/blog/article_form.twig">
    <operation>
      <search><![CDATA[{{ footer }}]]></search>
      <add position="before"><![CDATA[
        <script type="text/javascript" src="view/javascript/focsseo/focsseo-transliterator.js"></script>
        <script>
        var languages = {}
        {% for language in languages %}
        languages['{{ language.language_id }}'] = {
          source: '#input-name{{ language.language_id }}',
          copyTextTo: [
            '#input-meta-title{{ language.language_id }}'
          ],
          targets: [
            {
              selector: '#input-keyword',
              forceReplace: {% if keyword %}false{% else %}true{% endif %}
            }
          ],
          prefix: '{{ language.seo_url_prefix }}',
          postfix: '{{ language.seo_url_postfix }}'
        }
        {% endfor %}

        new FocSSeoTransliterator({
          languages: languages
        });
        </script>
      ]]></add>
    </operation>
  </file>
  <!-- / for fractalika -->

  <!-- change logic of default seo_url -->
  <file path="catalog/controller/startup/seo_url.php">
  	<!-- disable common/home -->
    <operation>
        <search><![CDATA[} elseif ($key == 'path') {]]></search>
        <add position="before"><![CDATA[
        } elseif ($key == 'route') {
          if ($data['route'] == 'common/home') {
            if ($this->model_extension_module_focsseo->isEnabled()
                && $this->model_extension_module_focsseo->getSetting('disable_common_home', false)
            ) {
              $url .= '/' . $this->model_extension_module_focsseo->getUrlPrefix($this->config->get('config_language_id'), false);
              unset($data[$key]);
            }
          }
          else {
            $query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "seo_url WHERE `query` = '" . $this->db->escape($data['route']) . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'");
            if ($query->num_rows > 0) {
              if ($this->model_extension_module_focsseo->isEnabled()) {
                $url = $this->model_extension_module_focsseo->getUrlPrefix($this->config->get('config_language_id'));
              }
              $url .= '/' . $query->rows[0]['keyword'];
            }
          }
				]]></add>
	  </operation>
    <!-- index -->
    <operation>
      <search><![CDATA[if (isset($this->request->get['_route_'])) {]]></search>
      <add position="after"><![CDATA[
      $this->load->model('extension/module/focsseo');
			if ($this->model_extension_module_focsseo->isEnabled()) {
				// unset($this->session->data['fsseo_current_route']);
				// unset($this->session->data['fsseo_current_param']);

				$language = $this->model_extension_module_focsseo->getLanguageFromRoute($this->request->get['_route_']);
				if ($language && trim($language['prefix']) != '') {
					$this->session->data['language'] = $language['code'];
					$this->language = new Language($language['directory']);
					$this->language->load($language['code']);
					$this->registry->set('language', $this->language);
					$this->config->set('config_language_id', $language['language_id']);
					$this->request->get['_route_'] = substr( $this->request->get['_route_'], strlen($language['prefix'].'/'));
				}
			}
      ]]></add>
    </operation>
    <!-- <operation>
      <search><![CDATA[if (!isset($this->request->get['route'])) {]]></search>
      <add position="before"><![CDATA[
      if (isset($this->request->get['route'])) {
        if ($this->model_extension_module_focsseo->isEnabled()) {
          $this->session->data['fsseo_current_route'] = $this->request->get['route'];
          $this->session->data['fsseo_current_param'] = '';
        }
      }
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$this->request->get['route'] = 'product/product';]]></search>
      <add position="after"><![CDATA[
      if ($this->model_extension_module_focsseo->isEnabled()) {
        $this->session->data['fsseo_current_param'] = 'product_id=' . $this->request->get['product_id'];
        $this->session->data['fsseo_current_route'] = $this->request->get['route'];
      }
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$this->request->get['route'] = 'product/category';]]></search>
      <add position="after"><![CDATA[
      if ($this->model_extension_module_focsseo->isEnabled()) {
        $this->session->data['fsseo_current_param'] = 'path=' . $this->request->get['path'];
        $this->session->data['fsseo_current_route'] = $this->request->get['route'];
      }
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$this->request->get['route'] = 'product/manufacturer/info';]]></search>
      <add position="after"><![CDATA[
      if ($this->model_extension_module_focsseo->isEnabled()) {
        $this->session->data['fsseo_current_param'] = 'manufacturer_id=' . $this->request->get['manufacturer_id'];
        $this->session->data['fsseo_current_route'] = $this->request->get['route'];
      }
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[$this->request->get['route'] = 'information/information';]]></search>
      <add position="after"><![CDATA[
      if ($this->model_extension_module_focsseo->isEnabled()) {
        $this->session->data['fsseo_current_param'] = 'information_id=' . $this->request->get['information_id'];
        $this->session->data['fsseo_current_route'] = $this->request->get['route'];
      }
      ]]></add>
    </operation> -->
    <!-- rewrite -->
    <operation>
      <search><![CDATA[public function rewrite($link) {]]></search>
      <add position="after"><![CDATA[
      $this->load->model('extension/module/focsseo');
      ]]></add>
    </operation>
    <!-- rewrites for product -->
    <operation>
      <search><![CDATA[$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'");]]></search>
      <add position="after"><![CDATA[
      if ($this->model_extension_module_focsseo->isEnabled()) {
        $url = $this->model_extension_module_focsseo->getUrlPrefix($this->config->get('config_language_id'));
      }
      ]]></add>
    </operation>
    <!-- rewrites for categories -->
    <operation>
      <search><![CDATA[$categories = explode('_', $value);]]></search>
      <add position="after"><![CDATA[
      if ($this->model_extension_module_focsseo->isEnabled()) {
        $url = $this->model_extension_module_focsseo->getUrlPrefix($this->config->get('config_language_id'));
      }
      ]]></add>
    </operation>
  </file>

  <!-- correct redirects on language switch -->
  <file path="catalog/controller/common/language.php">
    <operation>
      <search><![CDATA[$data['redirect'] = $this->url->link($route, $url, $this->request->server['HTTPS']);]]></search>
      <add position="replace"><![CDATA[
        $emptyUrl = new Url($this->config->get('site_url'), $this->config->get('site_ssl'));
        $data['redirect'] = $emptyUrl->link($route, $url, $this->request->server['HTTPS']);
      ]]></add>
    </operation>
    <operation>
      <search><![CDATA[if (isset($this->request->post['redirect'])) {]]></search>
      <add position="after"><![CDATA[
      $this->load->model('extension/module/focsseo');
      if ($this->model_extension_module_focsseo->isEnabled()) {
				if ($localized = $this->model_extension_module_focsseo->getRedirectUrl($this->session->data['language'], $this->request->post['redirect'])) {
					$this->request->post['redirect'] = $localized;
				}
      }
      ]]></add>
    </operation>
  </file>
</modification>